package com.map.station;

import java.util.ArrayList;
import java.util.List;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.OverlayItem;

import android.content.Intent;
import android.database.Cursor;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.os.Bundle;

public class CustomMarker2 extends MapActivity{
	
	private MapView mapView;
	private MapController mapControl;
	private static boolean satellite, streetView, traffic;
	
	private static Intent intent;
	private static Bundle bundle = new Bundle();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mapview);
		
		// get value Satellite, StreetView, Traffic on google map
		satellite = OptionPreference.setSatellite(getBaseContext());
		streetView = OptionPreference.setStreetView(getBaseContext());
		traffic = OptionPreference.setTraffic(getBaseContext());
		
		//get "searchkeyword" from search page
		final Bundle bundle = this.getIntent().getExtras();
		String rate = bundle.getString("searchKeyword");
		
		initMapView();
		initMyLocation();
		drawStation(rate);
	}
	
	// set Satellite, StreetView, Traffic
	private void initMapView() {
		mapView = (MapView)findViewById(R.id.mapView);
		mapControl = mapView.getController();
		mapView.setSatellite(satellite);
		mapView.setTraffic(traffic);
		mapView.setStreetView(streetView);
		//mapView.invalidate();
		mapView.setBuiltInZoomControls(true);
		
		mapControl = mapView.getController();
	}
	
	// get and mark point to current location
	private void initMyLocation() {
		final MyLocationOverlay overlay = new MyLocationOverlay(this, mapView);
		overlay.enableMyLocation();
		overlay.runOnFirstFix(new Runnable() {
			
			@Override
			public void run() {
				mapControl.setZoom(14);
				mapControl.animateTo(overlay.getMyLocation());
			}
		});
		mapView.getOverlays().add(overlay);
	}
	
	String param;
	
	// draw image station on google map
	private void drawStation(String param) {
		Drawable mark = null;
		this.param = param;
		MarkAdapter mad;
		
		//check user to choose service
		if(param.equalsIgnoreCase("proserv")) {
			mark = getResources().getDrawable(R.drawable.shell);
			mark.setBounds(0, 0, mark.getIntrinsicWidth(), mark.getIntrinsicHeight());
			mad = new MarkAdapter(mark, param);
			mapView.getOverlays().add(mad);
		} else if(param.equalsIgnoreCase("tiger mart")) {
			mark = getResources().getDrawable(R.drawable.esso);
			mark.setBounds(0, 0, mark.getIntrinsicWidth(), mark.getIntrinsicHeight());
			mad = new MarkAdapter(mark, param);
			mapView.getOverlays().add(mad);
		} else if(param.equalsIgnoreCase("seven eleven")) {
			mark = getResources().getDrawable(R.drawable.ptt);
			mark.setBounds(0, 0, mark.getIntrinsicWidth(), mark.getIntrinsicHeight());
			mad = new MarkAdapter(mark, param);
			mapView.getOverlays().add(mad);
		} else if(param.equalsIgnoreCase("tiny coffee")) {
			mark = getResources().getDrawable(R.drawable.shell);
			mark.setBounds(0, 0, mark.getIntrinsicWidth(), mark.getIntrinsicHeight());
			mad = new MarkAdapter(mark, param);
			mapView.getOverlays().add(mad);
		} else if(param.equalsIgnoreCase("kbank atm")) {
			mark = getResources().getDrawable(R.drawable.shell);
			mark.setBounds(0, 0, mark.getIntrinsicWidth(), mark.getIntrinsicHeight());
			mad = new MarkAdapter(mark, param);
			mapView.getOverlays().add(mad);
		} else if(param.equalsIgnoreCase("ktb bank")) {
			mark = getResources().getDrawable(R.drawable.shell);
			mark.setBounds(0, 0, mark.getIntrinsicWidth(), mark.getIntrinsicHeight());
			mad = new MarkAdapter(mark, param);
			mapView.getOverlays().add(mad);
		} else if(param.equalsIgnoreCase("visa card")) {
			Drawable shell = getResources().getDrawable(R.drawable.shell);
			Drawable esso = getResources().getDrawable(R.drawable.esso);
			Drawable ptt = getResources().getDrawable(R.drawable.ptt);
			
			shell.setBounds(0, 0, shell.getIntrinsicWidth(), shell.getIntrinsicHeight());
			esso.setBounds(0, 0, esso.getIntrinsicWidth(), esso.getIntrinsicHeight());
			ptt.setBounds(0, 0, ptt.getIntrinsicWidth(), ptt.getIntrinsicHeight());
			
			visaShell vs = new visaShell(shell);
			visaEsso ve = new visaEsso(esso);
			visaPtt vp = new visaPtt(ptt);
			
			mapView.getOverlays().add(vs);
			mapView.getOverlays().add(ve);
			mapView.getOverlays().add(vp);
		}
	}
	
	// get value for mark station on google map
	private class MarkAdapter extends ItemizedOverlay {
		
		private List<OverlayItem> location = new ArrayList<OverlayItem>();
		private Drawable marker;
		
		private Cursor c;
		private String param;
		private int key;
		private MarkAdapter(Drawable marker, String param) {
			super(marker);
			this.marker = marker;
			this.param = param;
			
			if(param.equalsIgnoreCase("seven eleven")) {
				param = "seven";
				key = 1;
			} else if(param.equalsIgnoreCase("tiger mart")) {
				param = "mart";
				key = 1;
			} else if(param.equalsIgnoreCase("Proserv")) {
				param = "proserv";
				key = 1;
			}else if(param.equalsIgnoreCase("VISA CARD")) {
				param = "visa";
				key = 1;
			}else if(param.equalsIgnoreCase("Tiny Coffee")) {
				param = "tiny";
				key = 2;
			} else if(param.equalsIgnoreCase("KBank ATM")) {
				param = "kbank";
				key = 2;
			} else if(param.equalsIgnoreCase("KTB Bank")) {
				param = "ktb";
				key = 2;
			}  
			
			// query data from DB
			DBAdapter db = new DBAdapter(getBaseContext());
			db.open();
			if(key == 1) {
				c = db.getService(param);
			} else if(key == 2) {
				c = db.getDetail(param);
			}
			if(c.moveToFirst()) {
				do {
					displayStation(c);
				} while (c.moveToNext());
			}
			db.close();
		}
		
		@Override
		public void draw(Canvas canvas, MapView mapView, boolean shadow) {
			super.draw(canvas, mapView, shadow);
			boundCenter(marker);
		}

		@Override
		protected OverlayItem createItem(int i) {
			return location.get(i);
		}

		@Override
		public int size() {
			return location.size();
		}
		
		//when tap on screen will be "Intent" to show detail
		@Override
		protected boolean onTap(int index) {
			intent = new Intent(getBaseContext(), DetailActivity.class);
			bundle.putString("stationKeyword", param);
			bundle.putInt("indexKeyword", index);
			intent.putExtras(bundle);
			startActivity(intent);
			return false;
		}
		
		//convert latitude and longitude to Double
		private void displayStation(Cursor c) {
			
			double clat = Double.parseDouble(c.getString(2));
			double clng = Double.parseDouble(c.getString(3));
			
			GeoPoint markStation = new GeoPoint((int)(clng*1000000)
					,(int)(clat*1000000));
			
			location.add(new OverlayItem(markStation, "service", "service"));
			populate();
		}
	}

	private class visaShell extends ItemizedOverlay {
		
		private List<OverlayItem> location = new ArrayList<OverlayItem>();
		private Drawable marker;
		private String key = "vshell";
		
		private visaShell(Drawable marker) {
			super(marker);
			this.marker = marker;
			
			GeoPoint visa1 = new GeoPoint((int)(7.868459*1000000)
					, (int)(98.356324*1000000));
			GeoPoint visa2 = new GeoPoint((int)(7.90827*1000000)
					, (int)(98.356324*1000000));

			location.add(new OverlayItem(visa1, "visa1", "visa1"));
			location.add(new OverlayItem(visa2, "visa2", "visa2"));
			populate();
		}
		
		@Override
		public void draw(Canvas canvas, MapView mapView, boolean shadow) {
			super.draw(canvas, mapView, shadow);
			boundCenterBottom(marker);
		}

		@Override
		protected OverlayItem createItem(int i) {
			return location.get(i);
		}

		@Override
		public int size() {
			return location.size();
		}
		
		@Override
		protected boolean onTap(int index) {
			Intent intent = new Intent(getBaseContext(), DetailActivity.class);
			bundle.putString("stationKeyword", key);
			bundle.putInt("indexKeyword", index);
			intent.putExtras(bundle);
			startActivity(intent);
			return false;
		}
	}

	private class visaPtt extends ItemizedOverlay {
		
		private List<OverlayItem> location = new ArrayList<OverlayItem>();
		private Drawable marker;
		private String key = "vptt";
		
		private visaPtt(Drawable marker) {
			super(marker);
			this.marker = marker;
			
			GeoPoint visa1 = new GeoPoint((int)(7.91028*1000000)
					, (int)(98.39176*1000000));

			location.add(new OverlayItem(visa1, "visa1", "visa1"));
			populate();
		}
		
		@Override
		public void draw(Canvas canvas, MapView mapView, boolean shadow) {
			super.draw(canvas, mapView, shadow);
			boundCenterBottom(marker);
		}

		@Override
		protected OverlayItem createItem(int i) {
			return location.get(i);
		}

		@Override
		public int size() {
			return location.size();
		}
		
		@Override
		protected boolean onTap(int index) {
			Intent intent = new Intent(getBaseContext(), DetailActivity.class);
			bundle.putString("stationKeyword", key);
			intent.putExtras(bundle);
			startActivity(intent);
			return false;
		}
	}

	private class visaEsso extends ItemizedOverlay {
		
		private List<OverlayItem> location = new ArrayList<OverlayItem>();
		private Drawable marker;
		private String key = "vesso";
		
		private visaEsso(Drawable marker) {
			super(marker);
			this.marker = marker;
			
			GeoPoint visa1 = new GeoPoint((int)(7.90199*1000000)
					, (int)(98.3687*1000000));

			location.add(new OverlayItem(visa1, "visa1", "visa1"));
			populate();
		}
		
		@Override
		public void draw(Canvas canvas, MapView mapView, boolean shadow) {
			super.draw(canvas, mapView, shadow);
			boundCenterBottom(marker);
		}

		@Override
		protected OverlayItem createItem(int i) {
			return location.get(i);
		}

		@Override
		public int size() {
			return location.size();
		}
		
		@Override
		protected boolean onTap(int index) {
			Intent intent = new Intent(getBaseContext(), DetailActivity.class);
			bundle.putString("stationKeyword", key);
			intent.putExtras(bundle);
			startActivity(intent);
			return false;
		}
	}
	
	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}
}
